package com.pengpeng.sheepfoldmall.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.pengpeng.sheepfoldmall.product.dao.BrandDao;
import com.pengpeng.sheepfoldmall.product.dao.CategoryDao;
import com.pengpeng.sheepfoldmall.product.entity.BrandEntity;
import com.pengpeng.sheepfoldmall.product.entity.CategoryEntity;
import com.pengpeng.sheepfoldmall.product.service.BrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pengpeng.common.utils.PageUtils;
import com.pengpeng.common.utils.Query;

import com.pengpeng.sheepfoldmall.product.dao.CategoryBrandRelationDao;
import com.pengpeng.sheepfoldmall.product.entity.CategoryBrandRelationEntity;
import com.pengpeng.sheepfoldmall.product.service.CategoryBrandRelationService;


@Service("categoryBrandRelationService")
public class CategoryBrandRelationServiceImpl extends ServiceImpl<CategoryBrandRelationDao, CategoryBrandRelationEntity> implements CategoryBrandRelationService {

    @Autowired
    private CategoryDao categoryDao;

    @Autowired
    private BrandDao brandDao;

    @Autowired
    private BrandService brandService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CategoryBrandRelationEntity> page = this.page(
                new Query<CategoryBrandRelationEntity>().getPage(params),
                new QueryWrapper<CategoryBrandRelationEntity>()
        );

        return new PageUtils(page);
    }

    /**
     * 保存（分类品牌关联关系的详细信息）
     * @param categoryBrandRelation
     */
    @Override
    public void saveDetail(CategoryBrandRelationEntity categoryBrandRelation) {
        //先获取品牌id和分类id
        Long brandId = categoryBrandRelation.getBrandId();
        Long catelogId = categoryBrandRelation.getCatelogId();

        //分别获取他们的名字
        BrandEntity brandEntity = brandDao.selectById(brandId);
        CategoryEntity categoryEntity = categoryDao.selectById(catelogId);

        //更新实体类中的数据
        categoryBrandRelation.setBrandName(brandEntity.getName());
        categoryBrandRelation.setCatelogName(categoryEntity.getName());

        baseMapper.insert(categoryBrandRelation);
    }

    /**
     * 用于主要的表（品牌表，分类表）更新后，级联更新表中的冗余字段
     * @param brandId
     * @param name
     */
    @Override
    public void updateBrand(Long brandId, String name) {
        //先赋值实体类
        CategoryBrandRelationEntity categoryBrandRelationEntity = new CategoryBrandRelationEntity();
        categoryBrandRelationEntity.setBrandId(brandId);
        categoryBrandRelationEntity.setBrandName(name);
        //更新 分类品牌关系表 中品牌名称
        baseMapper.update(categoryBrandRelationEntity,new LambdaUpdateWrapper<CategoryBrandRelationEntity>()
                .eq(CategoryBrandRelationEntity::getBrandId,brandId));
    }

    /**
     * 用于主要的表（分类表）更新后，级联更新表中的冗余字段
     * @param catId
     * @param name
     */
    @Override
    public void updateCategory(Long catId, String name) {
        //先赋值实体类
        CategoryBrandRelationEntity categoryBrandRelationEntity = new CategoryBrandRelationEntity();
        categoryBrandRelationEntity.setCatelogId(catId);
        categoryBrandRelationEntity.setCatelogName(name);
        //更新 分类品牌关系表 中分类名称
        baseMapper.update(categoryBrandRelationEntity,new LambdaUpdateWrapper<CategoryBrandRelationEntity>()
                .eq(CategoryBrandRelationEntity::getCatelogId,catId));
    }

    /**
     * 发布商品选择分类后回显分类所关联的品牌
     * @param catId
     * @return
     */
    @Override
    public List<BrandEntity> getBrandRelationByCatId(Long catId) {
        //先根据前端传来的分类id查询分类品牌关系表
        List<CategoryBrandRelationEntity> relationEntities = baseMapper.selectList(new LambdaQueryWrapper<CategoryBrandRelationEntity>()
                .eq(CategoryBrandRelationEntity::getCatelogId, catId));
        //根据得到的关系再去查询具体的品牌信息
        List<BrandEntity> brandEntityList = relationEntities.stream().map(item -> {
            BrandEntity brandEntity = brandService.getById(item.getBrandId());
            return brandEntity;
        }).collect(Collectors.toList());
        return brandEntityList;
    }
}